﻿#define _CRT_SECURE_NO_WARNINGS 1
//水果成篮：https://leetcode.cn/problems/fruit-into-baskets/description/
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int kind = 0;
        int n = fruits.size();
        int hash[100000] = { 0 };
        int ret = 0;

        for (int left = 0, right = 0; right < n; right++)
        {
            if (hash[fruits[right]] == 0)
            {
                kind++;
            }
            hash[fruits[right]]++;

            while (kind > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                {
                    kind--;
                }
                left++;
            }

            ret = max(ret, right - left + 1);    //一般都是等进入的这个数，通过了出窗口测试，才会更新数据
        }

        return ret;
    }
};

//找到字符串中所有字⺟异位词（medium）：https://leetcode.cn/problems/find-all-anagrams-in-a-string/
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> v;
        int hash1[26] = { 0 };
        int hash2[26] = { 0 };
        int m = p.size();
        for (auto a : p)
        {

            hash2[a - 'a']++;
        }

        for (int left = 0, right = 0, count = 0; right < s.size(); right++)
        {
            char in = s[right];
            hash1[in - 'a']++;
            if (hash1[in - 'a'] <= hash2[in - 'a'])   count++;

            if (right - left + 1 > m)
            {
                char out = s[left];
                if (hash1[out - 'a'] <= hash2[out - 'a'])   count--;

                hash1[out - 'a']--;
                left++;
            }

            if (count == m)
            {
                v.push_back(left);
            }
        }

        return v;
    }
};

//串联所有单词的⼦串（hard）：https://leetcode.cn/problems/substring-with-concatenation-of-all-words/submissions/564504880/
class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        unordered_map<string, int> hash1;
        vector<int> v;
        int len = words[0].size();
        int num = words.size();
        for (auto a : words)
        {
            hash1[a]++;
        }


 
        for (int i = 0; i < len; i++)
        {
            unordered_map<string, int> hash2;
            for (int right = i, left = i, count = 0; right + len - 1 < s.size(); right += len)
            {
                string in = s.substr(right, len);
                hash2[in]++;
                if (hash1.count(in) && hash2[in] <= hash1[in])     count++;

                if ((right - left) / len == num)
                {
                    string out = s.substr(left, len);
                    if (hash1.count(out) && hash2[out] <= hash1[out])     count--;

                    hash2[out]--;
                    left += len;
                }

                if (count == num)
                {
                    v.push_back(left);
                }
            }

        }

        return v;

    }
};

//三道题中后面两道题是滑动窗口的第二种题型，即窗口长度是定值的题型